package com.hbnu.iostream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 需求：
 *      图片复制
 *      原图片地址：F:\2309\Music\img\歌手图片\4.png
 *      目的图片地址：E:\2309\4_copy.png
 *
 * @author kittychen
 * @date 2025-09-30 16:37
 */
public class PictureCopy {

    public static void main(String[] args) {
        String srcPath = "F:" + File.separator + "2309" + File.separator + "Music" + File.separator + "img" + File.separator + "歌手图片" + File.separator + "4.png";
        String destPath = "E:" + File.separator + "2309" + File.separator + "4_copy.png";

        //创建对象
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        // 准备流资源
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            inputStream = new FileInputStream(srcFile);
            outputStream = new FileOutputStream(destFile);

            // 复制
            int temp = 0;
            while ((temp = inputStream.read()) != -1) {
                outputStream.write(temp);
            }

            System.out.println("文件复制完成...");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流资源
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
